
package com.interview.case04;

import java.math.BigDecimal;
import java.math.RoundingMode;

public class Main {
    public static void main(String[] args) {
        // 创建购物车
        ShoppingCart cartD = new ShoppingCart();

        // 顾客D购买的水果斤数
        int applesD = 6;        // 6斤苹果
        int strawberriesD = 8;  // 8斤草莓
        int mangoesD = 5;       // 5斤芒果

        // 将水果添加到购物车
        Fruit apple = new Apple();
        Fruit strawberry = new Strawberry();
        Fruit mango = new Mango();
        cartD.addFruit(apple, applesD);
        cartD.addFruit(strawberry, strawberriesD);
        cartD.addFruit(mango, mangoesD);

        // 配置促销策略：满100减10元
        Promotion thresholdPromotion = new ThresholdDiscountPromotion(new BigDecimal("100.00"), new BigDecimal("10.00"));
        cartD.addPromotion(thresholdPromotion);

        // 计算总价
        BigDecimal totalD = cartD.calculateTotalPrice();

        // 预期总价：
        // 苹果: 6 * 8.00 = 48.00
        // 草莓: 8 * 13.00 * 0.80 = 83.20
        // 芒果: 5 * 20.00 = 100.00
        // 小计: 48.00 + 83.20 + 100.00 = 231.20
        // 满100减10: 231.20 - 10.00 = 221.20 元
        BigDecimal expectedD = new BigDecimal("6").multiply(new BigDecimal("8.00"))
                                .add(new BigDecimal("8").multiply(new BigDecimal("13.00")).multiply(new BigDecimal("0.80")))
                                .add(new BigDecimal("5").multiply(new BigDecimal("20.00")))
                                .subtract(new BigDecimal("10.00"));

        // 设置小数点精度
        totalD = totalD.setScale(2, RoundingMode.HALF_UP);
        expectedD = expectedD.setScale(2, RoundingMode.HALF_UP);

        // 输出结果
        System.out.println("顾客D购买的总价: " + totalD + " 元");

        // 验证计算结果
        if (totalD.compareTo(expectedD) == 0) {
            System.out.println("测试通过！");
        } else {
            System.out.println("测试失败！预期: " + expectedD + " 元, 实际: " + totalD + " 元");
        }
    }
}
